package longrise.phone.com.bjjt_henan.Adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;

/**
 * Created by Administrator on 2017-8-25.
 */

public class ServiceCenterAdapter extends BaseAdapter {

    private Context mContext;

    private List<EntityBean> datas;

    public ServiceCenterAdapter(Context context) {
        this.mContext = context;
    }

    public void setDatas(List<EntityBean> datas) {
        this.datas = datas;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas == null ? 0 : datas.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_servicecenter_item, null);
            holder.iconIV = (ImageView) convertView.findViewById(R.id.scenter_item_tv_icon);
            holder.nameTV = (TextView) convertView.findViewById(R.id.scenter_item_tv_name);
            holder.addressTV = (TextView) convertView.findViewById(R.id.scenter_item_tv_address);
            holder.rBar = (RatingBar) convertView.findViewById(R.id.scenter_item_rbar);
            holder.rateTV = (TextView) convertView.findViewById(R.id.scenter_item_tv_rate);
            holder.distanceTV = (TextView) convertView.findViewById(R.id.scenter_item_tv_distance);
            convertView.setTag(holder);

            CommontUtils.setRatingBarHeight(mContext, holder.rBar, R.mipmap.ratingbar_select);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EntityBean bean = datas.get(position);
        String url = bean.getString("picture");
        if (TextUtils.isEmpty(url)) {
            url = "drawable://" + R.mipmap.icon_center_image;
        }
        ImageLoader.getInstance().displayImage(url, holder.iconIV, getOptions());

        holder.nameTV.setText(bean.getString("centername", ""));
        holder.addressTV.setText(bean.getString("centeraddress", ""));
        //评分
        float score = getRate(bean.getString("score"));
        holder.rBar.setRating(score);
        holder.rateTV.setText(bean.getString("score", "0.0") + "分");
        //距离
        float distance = getRate(bean.getString("distance"));
        if (distance > 1) {
            float num = Math.round(distance * 10) / 10f;//保留一位小数
            holder.distanceTV.setText(num + "km");
        } else {
            holder.distanceTV.setText(Math.round(distance * 1000) + "m");//取整
        }

        return convertView;
    }

    private DisplayImageOptions options;

    //实例化imageloader的参数
    public DisplayImageOptions getOptions() {
        if (null == options) {
            DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
            builder.cacheInMemory(true);
            builder.cacheOnDisk(true);
            builder.displayer(new RoundedBitmapDisplayer(10));//设置圆角
            builder.imageScaleType(ImageScaleType.EXACTLY_STRETCHED);
            builder.bitmapConfig(Bitmap.Config.RGB_565);
            options = builder.build();
        }
        return options;
    }

    private float getRate(String score) {
        float rate = 0;
        try {
            if (!TextUtils.isEmpty(score)) {
                rate = Float.parseFloat(score);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rate;
    }

    private class ViewHolder {
        private ImageView iconIV;

        private TextView nameTV;

        private TextView addressTV;

        private RatingBar rBar;

        private TextView rateTV;

        private TextView distanceTV;
    }
}
